package com.nl.controller.web.service;

import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.global.GlobalEnum;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.*;
import com.nl.controller.web.mapper.LoginMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private CommonService commonService;

    @SystemServiceLog(description = "用户登录", value = false)
    public SmsUsers login(String msisdn, String password) {
        SmsUsers user;
        List<SmsUsers> list = new ArrayList<>();
        try {
            list = loginMapper.login(msisdn);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("登录异常：" + msisdn);
            throw new SmsException("登录异常：" + msisdn);
        }

        if (list.size() == 0) {
            throw new SmsException(GlobalEnum.USER_NOT_EXIT);

        } else {
            user = list.get(0);
            String mgrId = user.getMgrId();
            if (mgrId != "" && mgrId != null) {
                String arr[] = mgrId.split(",");
                List mgrList = new ArrayList();
                for (int i = 0; i < arr.length; i++) {
                    SmsMgrInfo smsMgrInfo;
                    try {
                        smsMgrInfo = loginMapper.getMgrInfo(arr[i]);
                        mgrList.add(smsMgrInfo);
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error("获取用户项目信息异常" + arr[i]);
                        throw new SmsException("获取用户项目信息异常：" + arr[i]);
                    }
                }
                user.setList(mgrList);
            }
            if (!password.equals(user.getPassword())) {
                commonService.createSmsOperLog(user,"0","密码校验错误","1","登录");
                log.error("登录异常：" + msisdn);
                throw new SmsException(GlobalEnum.PASSWORD_ERROR);
            }
        }
        return user;
    }

    @SystemServiceLog(description = "获取菜单", value = false)
    public List<SmsFunction> getMenu(String id) {
        List<SmsFunction> list = new ArrayList<>();
        List<SmsFunction> listAll = new ArrayList<>();
        try {
            listAll = loginMapper.getMenu(id);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取菜单出现异常：" + id);
            throw new SmsException("获取菜单出现异常：" + id);
        }
        //先选出父节点，然后给父节点加上子节点
        for (SmsFunction function : listAll) {
            if ("1".equals(function.getNodeTag())) {
                log.info("fatherFunction:{}", function.toString());
                for (SmsFunction smsFunction : listAll) {
                    if (smsFunction.getFatherFunctionId().equals(function.getFunctionId())
                            && "0".equals(smsFunction.getNodeTag())) {
                        log.info("Function:{}", smsFunction.toString());
                        function.getList().add(smsFunction);
                    }
                }
                list.add(function);
            }
        }
        return list;
    }

    @SystemServiceLog(description = "发送短信", value = false)
    public int sendCode(Sms_DT dt) {
        int result;
        try {
            result = loginMapper.sendCode(dt);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("插入短信异常：" + dt.toString());
            throw new SmsException("插入短信异常：" + dt.toString());
        }
        return result;
    }

    @SystemServiceLog(description = "获取权限列表", value = false)
    public List<TreeInfo> getFuncitons() {
        TreeInfo paTreeInfo = null;
        TreeInfo treeInfo = null;
        List<TreeInfo> list = new ArrayList<>();
        List<SmsFunction> listAll;
        try {
            listAll = loginMapper.getFunctions();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取权限列表异常");
            throw new SmsException("获取权限列表异常");
        }

        for (SmsFunction paFunction : listAll) {
            if ("1".equals(paFunction.getNodeTag())) {
                paTreeInfo = new TreeInfo();
                paTreeInfo.setTitle(paFunction.getFunctionName());
                paTreeInfo.setValue(paFunction.getFunctionId());
                list.add(paTreeInfo);
                for (SmsFunction smsFunction : listAll) {
                    if (smsFunction.getFatherFunctionId().equals(paFunction.getFunctionId())
                            && "0".equals(smsFunction.getNodeTag())) {
                        treeInfo = new TreeInfo();
                        treeInfo.setTitle(smsFunction.getFunctionName());
                        treeInfo.setValue(smsFunction.getFunctionId());
                        paTreeInfo.getData().add(treeInfo);
                    }
                }
            }
        }
        return list;
    }

}
